import { useRequest } from 'umi';
import {
  queryAreaListAPI,
  queryDoorListAPI,
  addDoorAPI,
  queryPlanModeAPI,
  queryDoorInfoAPI,
  editDoorAPI,
  deleteDoorAPI,
  queryControllerTypeAPI,
  queryDoorOpenTypeAPI,
  sentDownPlanModeAPI,
  queryDoorOpenRecordAPI,
  queryAlarmListByDoorAPI,
  queryHisAlarmListByDoorAPI,
  setTimeAPI,
} from './services';
import { createModel } from 'hox';
import { useState } from 'react';
import { DataNode } from 'antd/lib/tree';
import { AreaListReturnProps } from './API';
type AreaListProps = DataNode & { code?: string };
interface HoxModelProps {
  areaList: AreaListProps[];
  selectedKeys: string[];
  currentCode: string;
  selectedRowKeys: (string | number)[];
  formAreaList: AreaListReturnProps[];
  isShowControllerTable: boolean;
  info: { id?: number; [key: string]: any };
}
function useHoxModel() {
  const [state, setState] = useState<HoxModelProps>({
    areaList: [],
    selectedKeys: [],
    currentCode: '',
    selectedRowKeys: [],
    formAreaList: [],
    isShowControllerTable: false,
    info: {},
  });
  const dispatch = (payload: Partial<HoxModelProps>) => {
    setState((pre) => ({ ...pre, ...payload }));
  };

  //获取表单区域接口
  const { run: getFormAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    onSuccess: (res) => {
      if (res.result === 0) {
        let data = res?.data;
        const formAreaList = data.map((item: any) => {
          return {
            label: item.name,
            value: item.id,
            isLeaf: item.isRegion,
          };
        });
        dispatch({ formAreaList });
      }
    },
    formatResult: (res) => res,
  });
  //下发计划模板
  const { run: sentDownPlanMode } = useRequest(sentDownPlanModeAPI, {
    manual: true,
  });
  //根据门来获取历史告警数据
  const { run: getHisAlarmListByDoor } = useRequest(queryHisAlarmListByDoorAPI, {
    manual: true,
  });
  //根据门来获取告警数据
  const { run: getAlarmListByDoor } = useRequest(queryAlarmListByDoorAPI, {
    manual: true,
  });
  //获取门的打开记录
  const { run: getDoorOpenRecord } = useRequest(queryDoorOpenRecordAPI, {
    manual: true,
  });
  //获取控制器类型接口
  const { run: getControllerType, data: controllerType } = useRequest(queryControllerTypeAPI, {
    manual: true,
  });
  //获取控制器类型接口
  const { run: getDoorOpenType, data: doorOpenType } = useRequest(queryDoorOpenTypeAPI, {
    manual: true,
  });
  //获取计划模板列表接口
  const { run: getPlanMode, data: planMode } = useRequest(queryPlanModeAPI, {
    manual: true,
  });
  //获取左边区域接口
  const { run: getAreaList, loading: getAreaListLoading } = useRequest(queryAreaListAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //添加门接口
  const { run: addDoor } = useRequest(addDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //删除用户接口
  const { run: deleteDoor } = useRequest(deleteDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //修改用户接口
  const { run: editDoor } = useRequest(editDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取用户详情接口
  const { run: getDoorInfo, data: doorInfo, loading: doorInfoLoading } = useRequest(
    queryDoorInfoAPI,
    {
      manual: true,
    },
  );
  //获取门设备列表接口
  const { run: getDoorList, data: doorList } = useRequest(queryDoorListAPI, {
    manual: true,
  });
  //设置本地时间
  const { run: setTime } = useRequest(setTimeAPI, {
    manual: true,
  });

  //第一次获取区域
  const firstGetAreaList = async () => {
    let currentArea = await getAreaList();
    if (currentArea && currentArea.result === 0) {
      //数据格式转换
      const areaList = currentArea.data.map((item: any) => {
        return {
          key: item.id.toString(),
          title: item.name,
          isLeaf: item.isRegion,
          code: item.code,
        };
      });
      //默认选中第一个
      let item = areaList[0];

      dispatch({ areaList, selectedKeys: [item.key], currentCode: item.code });
    }
  };
  return {
    ...state,
    dispatch,
    addDoor,
    getAreaList,
    editDoor,
    getDoorList,
    doorList,
    getDoorInfo,
    doorInfo,
    deleteDoor,
    doorInfoLoading,
    getPlanMode,
    getControllerType,
    controllerType,
    doorOpenType,
    getDoorOpenType,
    sentDownPlanMode,
    planMode,
    setTime,
    getFormAreaList,
    firstGetAreaList,
    getDoorOpenRecord,
    getAreaListLoading,
    getHisAlarmListByDoor,
    getAlarmListByDoor,
  };
}
export default createModel(useHoxModel);
